package com.theta.gateway.infra.common.base;


import lombok.Data;

import java.io.Serializable;

import com.theta.gateway.infra.common.exception.BusinessException;
import com.theta.gateway.infra.common.exception.enums.ErrorEnum;


/**
 * 响应数据
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final int SUCCESS=0;
	private static final int ERROR=500;
	
    /**
     * 编码：0表示成功，其他值表示失败
     */
	private int code = SUCCESS;

    /**
     * 消息内容
     */
    private String msg = "success";
    
    private boolean success;

    /**
     * 响应数据
     */
    private T data;

    public Result<T> ok(T data) {
        this.setData(data);
        return this;
    }

//    public boolean success() {
//        return code == 0;
//    }


//    public Result<T> error(int code, String msg) {
//        this.code = code;
//        this.msg = msg;
//        return this;
//    }
    
    public Result() {
		
	}
	
	public Result(int code) {
		this.code=code;
	}
	
	public Result(int code,String message) {
		this.code=code;
		this.msg=message;
	}
	
	public Result(int code,String message,T data) {
		this.code=code;
		this.success=this.code==SUCCESS;
		this.msg=message;
		this.data=data;
	}
	
	
	@SuppressWarnings("rawtypes")
	public static Result success(){
		Result result=new Result<>();
		result.setSuccess(true);
		return result;
	}
	
	public static <T> Result<T> success(T data){
		Result<T> result=new Result<>(SUCCESS);
		result.setSuccess(true);
		result.setData(data);
		return result;
	}
	
	
	public static <T> Result<T> error(int code,String message){
		Result<T> result=new Result<>(code,message);
		result.setSuccess(false);
		return result;
	}
	
	public static <T> Result<T> error(Exception e){
		return error(ERROR,e.getMessage());
	}
	
	public static <T> Result<T> error(BusinessException e){
		return error(e.getCode(),e.getMessage());
	}
	public static <T> Result<T> error(BusinessException e,T data){
		Result<T> result= error(e.getCode(),e.getMessage());
		result.setData(data);
		return result;
	}
	
	public static <T> Result<T> error(String message){
		return error(ERROR,message);
	}
	
	public static <T> Result<T> error(ErrorEnum errorEnum){
		return error(errorEnum.getCode(), errorEnum.getMessage());
	}
	
	
	
	

}
